package org.example.core.mysql.convert;

import org.example.core.exception.ErrorException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAccessor;
import java.util.Date;

public class DateTimeToStringConvert extends AbstractConvert {

    @Override
    public Object convert(Object value) {
        if (value instanceof LocalDateTime){
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")
                    .withZone(ZoneId.of("Asia/Shanghai"));
            LocalDateTime localDateTime = (LocalDateTime) value;
            ZonedDateTime zonedDateTime = localDateTime.atZone(ZoneId.of("CTT", ZoneId.SHORT_IDS));
            return zonedDateTime.format(formatter);
        }else if (value instanceof String){
            LocalDateTime result = LocalDateTime.parse((String) value,DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            return result;
        }
        return value;
    }
}
